<?php
class Administrator_TagController extends QSource_System_Controller_Action{
	public function init(){
		$this->loadTemplate('administrator');
	}
	public function indexAction(){ 
	    $model = new Model_Tag_Tag();
	    $data = $model->getAll();
	    Zend_View_Helper_PaginationControl::setDefaultViewPartial(APPLICATION_PATH.'\modules\administrator\views\pagination.phtml');
	    $pa = Zend_Paginator::factory($data);
	    $pa->setItemCountPerPage(10);
	    $current = $this->_request->getParam('page');
	    $this->view->page = $current;
	    $pa->setCurrentPageNumber($current);
	    $this->view->data = $pa;
	}
	public function addAction(){
		$model = new Model_Tag_Tag();
		if ($this->_request->isPost()) {
		    $error = array();
			$data = $this->_request->getParams();
			if ($data['name'] == '')
				$error['name'] = ' Tên thể loại không được rỗng';
			if (count($error) > 0) {
				$this->view->error = $error;
				$this->view->data = $data;
			} else {
				$item = array('name'=>$data['name']);
				$model->insert($item);
				$this->view->success = 'Đã thêm thể loại mới thành công';
			}
			
		}
	}
	public function editAction(){
		$model = new Model_Tag_Tag();
		$id = $this->_request->getParam('id','');
		if ($this->_request->isPost()) {
			$error = array();
			$data = $this->_request->getParams();
			if ($data['name'] == '')
				$error['name'] = ' Tên thể loại không được rỗng';
			if (count($error) > 0) {
				$this->view->error = $error;
				$this->view->data = $data;
			} else {
				$item = array('name'=>$data['name']);
				$model->update($item,'id = '.$id);
				$this->_redirect('/administrator/tag');
			}
				
		}
		else
		{
		   $data = $model->getItemId($id);
		   if(sizeof($data) > 0)
		   $this->view->data = $data;
		}
	}
	public function searchAction(){
		$keyword = $this->_request->getParam('keyword','');
		if($keyword == '')
			$this->_redirect('/administrator/tag');
		$model = new Model_Tag_Tag();
		$data = $model->getAllStartWith($keyword);
		Zend_View_Helper_PaginationControl::setDefaultViewPartial(
		APPLICATION_PATH . '\modules\administrator\views\pagination.phtml');
		$pa = Zend_Paginator::factory($data);
		$pa->setItemCountPerPage(10);
		$current = $this->_request->getParam('page');
		$this->view->page = $current;
		$pa->setCurrentPageNumber($current);
		$this->view->data = $pa;
		$this->renderScript('tag/index.phtml');
	}
	
	//Data name Json - Data autocomplete for jqueryui
	public function datanamejsonAction(){
		$model = new Model_Tag_Tag();
		$term = $this->_request->getParam('term','');
		if($term=='')
			$data = $model->getAll();
		else
			$data = $model->getAllStartWith($term);
		$this->view->data = $data;
		$this->_helper->layout->disableLayout();
	}
	
	public function removeAction ()
	{
		Zend_Layout::resetMvcInstance();
		$this->_helper->viewRenderer->setNoRender();
		$id = $this->_request->getParam('id', '');
		if ($id != '') {
			$model = new Model_Tag_Tag();
			$model->removeTag($id);
		}
		$this->_redirect('/administrator/tag');
	}
	
}